package bat.iog.hytool.fileHandler.zip;


import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.io.outputstream.ZipOutputStream;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.ZipModel;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.EncryptionMethod;

import java.io.File;
import java.util.List;

public class ZIPUtil {

    //zip文件加密码
    public static void zipAddPassword() throws ZipException {
        
        String filePath = "D:\\Test\\2022-09-20.zip";
        //File file = new File(filePath);
        char[] password = "123456".toCharArray();
        
        ZipFile zipFile = new ZipFile(filePath, password);
        
        
        List<FileHeader> list = zipFile.getFileHeaders();
        for (FileHeader obj : list) {
            obj.setEncrypted(true);
        }
        
        ZipParameters parameters = new ZipParameters();
        parameters.setEncryptFiles( true );
        
        System.out.println(zipFile.isValidZipFile());
        System.out.println(zipFile.isEncrypted());
        
        
        zipFile.setPassword(password);
        System.out.println("123");
    }

    // 创建带有密码的压缩文件
    public static void createZipFileAddPassword(){

        ZipParameters zipParameters = new ZipParameters();
        zipParameters.setEncryptFiles(true);
        zipParameters.setCompressionLevel(CompressionLevel.HIGHER);
        zipParameters.setEncryptionMethod(EncryptionMethod.AES);
        String filePath = "D:\\Test\\2022-09-20.zip";
        ZipFile zipFile = new ZipFile(filePath, "123456".toCharArray());
        //ZipOutputStream outputStream = new ZipOutputStream(os, new ZipModel());
    }
    
    
    public static void main(String[] args) {
        try {
            zipAddPassword();
        } catch (ZipException e) {
            e.printStackTrace();
        }
    }
    
//    private static void zipFile() throws ZipException {
//        // 生成的压缩文件
//        ZipFile zipFile = new ZipFile("D:\\test.zip");
//        ZipParameters parameters = new ZipParameters();
//        // 压缩方式
//        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
//        // 压缩级别
//        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
//        parameters.setEncryptFiles( true );
//        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
//        parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
//        parameters.setPassword("111");
//        // 要打包的文件夹
//        File currentFile = new File("D:\\test");
//        File[] fs = currentFile.listFiles();
//        // 遍历test文件夹下所有的文件、文件夹
//        for (File f : fs) {
//            if (f.isDirectory()) {
//                zipFile.addFolder(f.getPath(), parameters);
//            } else {
//                zipFile.addFile(f, parameters);
//            }
//        }
//    }
//
//    public void zip(HttpServletRequest request, HttpServletResponse response) {
//        try {
//            //获取输出到页面的流
//            response.setContentType("application/x-msdownload;charset=utf-8");
//            response.addHeader("Content-Disposition", "attachment;filename=" + "aaa.zip");
//            response.setBufferSize(81920);
//            
//            OutputStream os = response.getOutputStream();
//            
//            //将nas文件读取到流中
//            FileInputStream fis = new FileInputStream("F:\\aaa.zip");
//            ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
//            byte[] b = new byte[1024];
//            int n;
//            int off = 0;
//            while ((n = fis.read(b)) != -1) {
//                bos2.write(b, off, n);
//            }
//            fis.close();
//            
//            ZipOutputStream outputStream = new ZipOutputStream(os, new ZipModel());
//            ZipParameters parameters = new ZipParameters();
//            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
//            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
//            parameters.setSourceExternalStream(true);
//            parameters.setFileNameInZip("123.zip");
//            parameters.setEncryptFiles(true);
//            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
//            parameters.setPassword("1234");
//            outputStream.putNextEntry(null, parameters);
//            outputStream.write(bos2.toByteArray());
//            
//            bos2.flush();
//            bos2.close();
//            outputStream.flush();
//            outputStream.closeEntry();
//            outputStream.finish();
//            outputStream.close();
//            os.flush();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//    
//    /**
//     * 加密1，winrar，好压2345可直接输入密码解压
//     * 根据filepath读取文件并加密返回
//     */
//    public static void zipFileAndEncrypt(String filePath,String zipFileName,String password) {
//        try {
//            ZipParameters parameters = setParam(password);
//            //压缩文件,并生成压缩文件
//            ArrayList<File> filesToAdd = new ArrayList<File>();
//            File file = new File(filePath);
//            filesToAdd.add(file);
//
//            ZipFile zipFile = new ZipFile(zipFileName);
//            zipFile.addFiles(filesToAdd, parameters);//this line does works
//            System.err.println("end");
//        } catch (ZipException e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 加密1，winrar，好压2345可直接输入密码解压
//     */
//    public static void zipFileStream(InputStream is, String zipFileName, String password) {try {
//        ZipParameters parameters = setParam(password);
//        //addStream,多设置两个参数，缺一不可
//        parameters.setFileNameInZip("yourfilename.xlsx");
//        parameters.setSourceExternalStream(true);
//
//        ZipFile zipFile = new ZipFile(zipFileName);
//        zipFile.addStream(is, parameters);
//    } catch (ZipException e) {
//        e.printStackTrace();
//    }
//    }
//    public static ZipParameters setParam(String password){
//        //设置压缩文件参数
//        ZipParameters parameters = new ZipParameters();
//        //设置压缩方法
//        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
//        //设置压缩级别
//        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
//        //设置压缩文件是否加密
//        parameters.setEncryptFiles(true);
//        //设置aes加密强度
//        parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
//        //设置加密方法
//        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
//        //设置密码
//        parameters.setPassword(password.toCharArray());
//        return parameters;
//    }
//
////    public static void main(String[] args) throws Exception {
////        try{
////            long l1 = System.currentTimeMillis();
////
//////           //加密,addFile
//////           ZipComplexCipherUtil.zipFileAndEncrypt("D:\\aaa\\zhmm_db.xlsx","D:\\aaa\\ccc.zip","123");
////
//////          加密，addStream
////            InputStream in = new FileInputStream("D:\\aaa\\zhmm_db.xlsx");
////            //ZipComplexCipherUtil.zipFileStream(in,"D:\\aaa\\ccc.zip","123");
////
////            long l2 = System.currentTimeMillis();
////            System.out.println((l2 - l1) + "毫秒.");
////            System.out.println(((l2 - l1) / 1000) + "秒.");
////        }catch(Exception ex){
////            ex.printStackTrace();
////        }
//
////    }
    
}
